import requests as rq
from bs4 import BeautifulSoup as bs
import pandas as pd
#导入数据库接口
from pymongo import MongoClient 
#创建连接本地数据库
client = MongoClient() 
#创建数据库
database = client['maoyan']
collection = database['maoyandata']
#伪装浏览器
header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}

data = []
for i in range(0,100,10):
    url = 'https://maoyan.com/board/4?offset='+str(i)
    sources = rq.get(url,headers = header).text #获取网页源代码
    soup = bs(sources,'html.parser') #html.parser是解析器
    
    #根据以上分析方法得到更加快捷的方式获取网站信息
    ranks = soup.find_all('i',class_="board-index")
    names = soup.find_all('p',class_="name")
    stars = soup.find_all('p',class_="star")
    time = soup.find_all('p',class_="releasetime")
    scores = soup.find_all('p',class_="score")    
    
    for r,n,s1,t,s2 in zip(ranks,names,stars,time,scores):
        rank = r.get_text()
        name = n.get_text().strip()
        star = s1.get_text().strip()[3:]
        time = t.get_text()[5:]
        score = s2.get_text()
            
        maoyan={'rank':rank,'name':name,'star':star,'time':time,'score':score}
        data.append(maoyan)
        print('{} {} {} {} {}'.format(rank,name,star,time,score))

#保存数据
#data = pd.DataFrame(data)
#index = ['排名','电影','演员','上映时间','评分'] 
#data.to_excel('猫眼电影榜单数据.xlsx',index = 0,header = index)

#保存在MongoDB上 --一次性保存
collection.insert(data)





